package com.itheima.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 写servet
 *  1.继承HttpServlet
 *  2.重写doXXX
 *  3.写访问路径 / 开头，代表Web应用下的资源
 */
@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet....");
        // 读请求行
        System.out.println(req.getMethod());
        System.out.println(req.getContextPath());
        System.out.println(req.getRequestURL());
        System.out.println(req.getRequestURI());
        // 读请求头
        System.out.println(req.getHeader("User-Agent"));
        // 读取请求行中的数据
        System.out.println(req.getQueryString());
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost....");
        // 如果是Post请求，请求是文本，我们使用流读数据
//        BufferedReader reader = req.getReader();
//        String s = reader.readLine();
//        System.out.println(s);
        // s就是请求体的原始数据，就一个字符串 K=v&k=v.K就是我们表单组件的name
        // 获取用户名，获取密码，连接数据库，判断是否登录
        // username=admin&password=1234 从这里面提前有效数据(字符串的切割与解析)
        String data = "username = admin & password=1234 & email=qq@163.com";
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // 我们不要自己解析
        System.out.println(username);
        System.out.println(password);

    }
}
